<?php
	session_start();

	viewPhotos();

	function viewPhotos(){
		$userName = $_SESSION['userName'];

		$userDir = "../users/".$userName;

		// Create images array
		$response = array(
			"success" => false,
			"images" => array()
		);


		// Make sure requesting user exists
		if(is_dir($userDir)){
			// Make sure requested album exists
			if($albumHandle = opendir($userDir)){
				// While there is another album directory, look at it
				while( false !== ($albumEntry = readdir($albumHandle))){
					$albumDir = $userDir."/".$albumEntry;
					if($handle = opendir($albumDir)){
						// While the is another file in the directory , look at it
						while( false !== ($entry = readdir($handle))){
							$file = $albumDir."/".$entry;
							$imageType = "jpeg";
							// Make sure that this is a file and a jpeg file
							if(is_file($file) && ($file = glob($albumDir.'/*.{jpg,jpeg,gif,bmp,png}', GLOB_BRACE))){
								// Add the images to the response object for javascript to use
								array_push($response["images"], "./users/".$userName."/".$albumEntry."/".$entry);
								$response["success"] = true;
							}
						}
					}
				}
			}
		}

		echo json_encode($response);
	}
?>
